#include "platfMsg_handle.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <pthread.h>

INSTALL_ARGS_STR p_installArgs = {0};
TRAV_RECORD_DOWN trav_record = {0};
extern PASS_MSG pRecvMsg;

INT32 split_platfMsg(INT8 *pIn, INT8 *pOut, INT8 c)
{
	INT32 msgLen;
	INT8 *p = NULL;

	if(pIn == NULL || pOut == NULL)
		return -1;

	p = memchr(pIn, c, strlen((const char *)pIn));
	msgLen = p - pIn;
	memset(pOut, 0, strlen((const char *)pOut));
	memcpy(pOut, pIn, msgLen);	

	return (msgLen + 1);
}

INT32 send_msg_platf(INT32 sfd, INT8 *pIn)
{
	INT32 r;

	do{
		r = send(sfd, pIn, strlen((const char *)pIn), 0);
	}
	while(r == -1 && errno == EINTR);
	if(r == -1){
		E(" Reply_msg_platf error.");
		return -1;
	}
	return 0;
}

INT32 platfMsg_handle(INT8 *pIn, INT32 datLen, INT32 sfd)
{
	INT32 ret, fd, flags = 0;
	UINT16 msgId = 0, len, ix;
	INT8 rpyPlatf[MAX_PKG_BODY_LEN] = {0}, sim[13] = {0};
	INT8 *p, flg[4] = {0}, msgbody[MAX_PKG_BODY_LEN] = {0};
	UINT8 pbody[MAX_PKG_LEN*4] = {0};

	memcpy(flg, pIn, 2);
	flags = strtoul((const char *)flg, NULL, 10);

	E(" flags: %d", flags);
	p = memchr(pIn+3, ',', datLen-4);
	if(p == NULL){
		memcpy(sim, pIn+3, datLen-4);
	}
	else	
		len = split_platfMsg(pIn+3, sim, ',');

	memset(p_installArgs.terminalTel, 0, 6);
	ret = simStr_convert_hex(sim, p_installArgs.terminalTel);

	for(ix = 0; ix < 6; ix++)
	{
		E(" %02x", p_installArgs.terminalTel[ix]);
	}
	memcpy(msgbody, pIn+3+len, datLen-(3+len));
	pthread_mutex_init(&pRecvMsg.mutex, NULL);

	switch(flags)
	{
		case 8:
			msgId = 0x8103;
			ret = setTerm_args(msgbody, pbody);
			if(ret == -1){
				E(" SetTerm_args analy error.");
				return -1;
			}
			break;
		case 9:
			msgId = 0x8104;
			break;
		case 11:
			msgId = 0x8105;
			ret = terminal_control(msgbody, pbody);
			if(ret == -1){
				E(" Terminal_control analy error.");
				return -1;
			}
			break;
		case 13:
			msgId = 0x8201;
			fd = open(NAME_FIFO_2, O_RDONLY | O_NONBLOCK, 0);
			if(fd == -1){
				perror("open().");
				exit(1);
			}
			if((ret = read(fd, rpyPlatf, sizeof(rpyPlatf))) == -1)
			{
				if(errno == EAGAIN){
					E(" Fifo2 not data yet.");
				}
				else
					return -1;
			}
			ret = send_msg_platf(sfd, rpyPlatf);
			if(ret == -1){
				E(" Reply_msg_platf error.");
				return -1;
			}
			close(fd);
			break;
		case 15:
			msgId = 0x8202;
			ret = temp_posTrack(msgbody, pbody);
			if(ret == -1){
				E(" Temp_posTrack error.");
				return -1;
			}
			break;
		case 16: 
			msgId = 0x8300;
			ret = txtInfo_send(msgbody, pbody);
			if(ret == -1){
				E(" TxtInfo_send error.");
				return -1;
			}
			break;
		case 17:
			msgId = 0x8301;
			ret = event_set(msgbody, pbody);
			if(ret == -1){
				E(" Event_set analy error.");
				return -1;
			}
			break;
		case 19:
			msgId = 0x8302;
			ret = question_send(msgbody, pbody);
			if(ret == -1){
				E(" Question_send analy error.");
				return -1;
			}
			break;
		case 21:
			msgId = 0x8303;
			ret = info_on_demand(msgbody, pbody);
			if(ret == -1){
				E(" Info_on_demand analy error.");
				return -1;
			}
			break;
		case 23:
			msgId = 0x8304;
			ret = info_service(msgbody, pbody);
			if(ret == -1){
				E(" Info_service analy error.");
				return -1;
			}
			break;
		case 24:
			msgId = 0x8400;
			ret = call_back(msgbody, pbody);
			if(ret == -1){
				E(" Call_back analy error.");
				return -1;
			}
			break;
		case 25:
			msgId = 0x8401;
			ret = set_contact(msgbody, pbody);
			if(ret == -1){
				E(" set_contact analy error.");
				return -1;
			}
			break;
		case 26:
			msgId = 0x8500;
			ret = vehicle_control(msgbody, pbody);
			if(ret == -1){
				E(" Vehicle_control error.");
				return -1;
			}
			break;
		case 28:
			msgId = 0x8600;
			ret = setCircle_area(msgbody, pbody);
			if(ret == -1){
				E(" SetCircle_area analy error.");
				return -1;
			}
			break;
		case 29:
			msgId = 0x8601;
			ret = delete_area_line(msgbody, pbody);
			if(ret == -1){
				E(" Delete circle area analy error.");
				return -1;
			}
			break;
		case 30:
			msgId = 0x8602;
			ret = setRect_area(msgbody, pbody);
			if(ret == -1){
				E(" SetRect_area analy error.");
				return -1;
			}
			break;
		case 31:
			msgId = 0x8603;
			ret = delete_area_line(msgbody, pbody);
			if(ret == -1){
				E(" Delete rect area analy error.");
				return -1;
			}
			break;
		case 32:
			msgId = 0x8604;
			ret = setPolygon_area(msgbody, pbody);
			if(ret == -1){
				E(" SetPolygon_area analy error.");
				return -1;
			}
			break;
		case 33:
			msgId = 0x8605;
			ret = delete_area_line(msgbody, pbody);
			if(ret == -1){
				E(" Delete polygon area analy error.");
				return -1;
			}
			break;
		case 34:
			msgId = 0x8606;
			ret = set_route(msgbody, pbody);
			if(ret == -1){
				E(" Set_route analy error.");
				return -1;
			}
			break;
		case 35:
			msgId = 0x8607;
			ret = delete_area_line(msgbody, pbody);
			if(ret == -1){
				E(" Delete line analy error.");
				return -1;
			}
			break;
		case 36:
			msgId = 0x8700;
			ret = traver_record_cmd(msgbody, pbody);
			if(ret == -1){
				E(" Traver_record_cmd analy error.");
				return -1;
			}
			break;
		case 38:
			msgId = 0x8701;
			ret = traver_record_down(msgbody, pbody);
			if(ret == -1){
				E(" Traver_record_down analy error.");
				return -1;
			}
			break;
		case 43:
			msgId = 0x8800;
			ret = media_data_upRepy(msgbody, pbody);
			if(ret == -1){
				E(" Media_data_upRepy analy error.");
				return -1;
			}
			break;
		case 44:
			msgId = 0x8801;
			ret = take_picture_immed(msgbody, pbody);
			if(ret == -1){
				E(" Take_picture_immed analy error.");
				return -1;
			}
			break;
		case 45:
			msgId = 0x8802;
			ret = save_media_data(msgbody, pbody);
			if(ret == -1){
				E(" Save_media_data analy error.");
				return -1;
			}
			break;
		case 47:
			msgId = 0x8803;
			ret = save_medata_upCmd(msgbody, pbody);
			if(ret == -1){
				E(" Save_medata_upCmd analy error.");
				return -1;
			}
			break;
		case 48:
			msgId = 0x8804;
			ret = start_tape_cmd(msgbody, pbody);
			if(ret == -1){
				E(" Start_tape_cmd analy error.");
				return -1;
			}
			break;
		case 49:
			msgId = 0x8900;
			ret = data_down_penetrate(msgbody, pbody);
			if(ret == -1){
				E(" Data_down_penetrate analy error.");
				return -1;
			}
			break;
		case 52:
			msgId = 0x8A00;
			ret = platf_RSA_key(msgbody, pbody);
			if(ret == -1){
				E(" Platf_RSA_key analy error.");
				return -1;
			}
			break;
		default:
			break;
	}
	ret = send(sfd, "success#", sizeof("success#"), 0);

	pthread_mutex_lock(&pRecvMsg.mutex);
	memcpy(pRecvMsg.pbody, pbody, sizeof(pRecvMsg.pbody));
	memcpy(pRecvMsg.sim, pIn+4, 11);
	pRecvMsg.msgId = msgId;
	pthread_mutex_unlock(&pRecvMsg.mutex);	

	fd = open(NAME_FIFO_1, O_WRONLY | O_NONBLOCK, 0);
	if(fd == -1){
		if(errno == ENXIO)
			E(" Open fifo1 error: no reading process.");
	}
	if((ret = write(fd, &pRecvMsg, sizeof(pRecvMsg))) == -1);
	{
		if(errno == EAGAIN){
			E(" The fifo1 has not been read yet.Please try later.");
		}
		else
			return -1;
	}	
	close(fd);
	return 0;
}
